<?
include_once "./src/facebook.php";
include_once "./Post.php";
include_once "./MultiRequest.php";

class FacebookQuery {
 
  private $facebook;
  private $request; 
  private $token;
  private $since = '1262304000';
  private $until = 'now';
  private $limit = 100;

  public function __construct(Facebook $facebook)
  {
		$this -> facebook = clone $facebook;
		$this -> request = new MultiRequest();
		$this -> token = $this -> facebook -> getAccessToken();
  }
  
  public function setFacebookObj(Facebook $facebook)
  {
		$this -> facebook = clone $facebook;
  }
  
  public function setSince ($date) {
	  $this -> since = $date;
  }
  public function setUntil ($date) {
	  $this -> until = $date;	  
  }
  public function setLimit ($limit) {
	  $this -> limit = $limit;	  
  }
  
  public function myInformations()
  {
	  return $this -> facebook -> api('/me');
  }
  
  
  public function allPosts($IDuser)
  {	  
	  $comments = $likes = 0;
	  // array definition
	  $all_posts = array();

	  $association = array();
	  $req_links = array ();

	  $index = 0;

	  // add the link of all posts in the array of requeste links.
	  array_push($req_links, 'https://graph.facebook.com/'.$IDuser.'/posts?limit='.$this -> limit.'&since='.$this -> since.'&until='.$this -> until.'&access_token='.$this-> token);
	  array_push($association, array('n' => $index++, 'type' => 'posts'));	    
	  $nextPage = true;

	  // while there are other requests execute them and store the informations
	  while (count($req_links) > 0)
	  {
		if (count($req_links) > 0) {
//			echo "<br />begin request $n of posts at ".date("D M j G:i:s T Y")."with request:<br />";
			$response = $this -> request -> multiRequest($req_links);
//			echo "end of request $n of posts at ".date("D M j G:i:s T Y")."<br />";
		}

		$req_links = array ();
		$posts = NULL;

		// Take the response and work on it.
		$index = 0;
		
		if ($association[$index]['type'] == 'posts')
			$posts = json_decode($response[$index++],true);
		  
		// work on the response that contains informations about the previous posts
		for (; $index < count($response); $index++) {

			//find the id of the post on which work
			$id = $association[$index]['id'];
			
			//take the content of the response
			$content = json_decode($response[$index],true);

			
			//find in the all_post array the post_obj that I want
			foreach ($all_posts as $tmp)
			  if ($tmp -> id == $id) {
			    $current_obj = $tmp;
			}
			
			if ($association[$index]['type'] == 'comment') {
//			  echo "aggiunti commenti<br>";
			  $current_obj -> comments = $content['data'];
			}

			if ($association[$index]['type'] == 'like') {
//			  echo "aggiunti likes<br>";
			  $current_obj -> likes = $content['data'];
			}
		}
				
		// now I can get the new content.
		$index = 0;
		$association = array();

		// Add next page request (if present)
  		$nextPage = $posts['paging']['next'];
		if($nextPage)
		{
			array_push($req_links, $nextPage.'&since='.$this->since);
			array_push($association, array('n' => $index++, 'type' => 'posts'));
		}

//		echo '<pre>'.print_r($posts['data'], true).'</pre><br><br><br><br>FINE<br><br><br><br><br>';

		// Work on the single posts
		foreach ($posts['data'] as $current_post)
		{
			$post_obj = new Post();
			$post_obj -> id = $current_post['id'];
		
			$time = strtotime($current_post['created_time']);
			$myDate = date("Y-m-d H:i:s", $time );
			
//			$post_obj -> created_time = $current_post['created_time'];
			$post_obj -> created_time = $myDate;
	
			$post_obj -> message = $current_post['message'];
			$post_obj -> numLikes = $current_post['likes']['count'];
			$post_obj -> numComments = $current_post['comments']['count'];
			if ($current_post['message_tags'])
				$post_obj -> tags = $this -> getTaggedPeople($current_post['message_tags']);
			
			$post_obj -> type = $current_post['type'];
			$post_obj -> story = $current_post['story'];
			$post_obj -> application = $current_post['application'];
			
			if ($current_post['comments']['count'] > count($current_post['comments']['data']))
			{
				$tmp_link = $this -> getLink($current_post['id'], 'comments', $current_post['comments']['count']);
				array_push($req_links, $tmp_link);
				array_push($association, array('n' => $index++, 'type' => 'comment', 'id' => $current_post['id']));				
				$comments++;
			}
			else {
				$post_obj -> comments = $current_post['comments']['data'];
			}
			if ($current_post['likes']['count'] > count($current_post['likes']['data']))
			{
				$tmp_link = $this -> getLink($current_post['id'], 'likes', $current_post['likes']['count']);
				array_push($req_links, $tmp_link);
				array_push($association, array('n' => $index++, 'type' => 'like', 'id' => $current_post['id']));
				$likes++;
			}
			else {
				$post_obj -> likes = $current_post['likes']['data'];
			}
			
			// Add the new post to the array of all posts
			array_push($all_posts, $post_obj);
		}
	  }
	  return $all_posts;
  }

  public function getLink ($id_post, $type, $count) {
	  return 'https://graph.facebook.com/'.$id_post.'/'.$type.'?limit='.$count.'&access_token='.$this-> token;
  }

  private function getTaggedPeople($tagsArray)
  {
	  $tagsPeople = array();
	  
	  foreach ($tagsArray as $current)
	  	array_push($tagsPeople, array('id' => $current[0]['id'], 'name' => $current[0]['name']));
		
	  return $tagsPeople;
  }


}










